package com.demo2do.fms.admin.entity.report.supplier;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.WeakHashMap;

import org.springframework.jdbc.core.RowMapper;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;

/**
 *
 * @author Ark
 *
 */
public class TrailerReportSearch implements QueryBuilder {

    /** 所属供方 */
    private Long supplierId;
    
    /** 车辆同步状态 */
    private String status;
    
    private QueryOrder queryOrder;
    
    private QueryFilter queryFilter;
    
    /**
     * The default constructor
     */
    public TrailerReportSearch() {
        queryOrder = new QueryOrder("t.brand_cn_code", "ASC");
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public QueryFilter getPreparedFilter(boolean refresh) {
        
        if (queryFilter == null || refresh) {
            
            queryFilter = new QueryFilter().addEquals("s", "id", supplierId)
                                           .addEquals("t", "status", status);
            
        }
        
        return queryFilter;
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        
        StringBuffer buffer = new StringBuffer(512);
        
        buffer.append("SELECT t.id AS id, ")
                     .append("t.brand_cn_code AS brandCnCode, ")
                     .append("t.floor_lord AS floorLord, ")
                     .append("t.vin AS vincode, ")
                     .append("s.name AS supplierName, ")
                     .append("s.short_name AS supplierSimpleName, ")
                     .append("CASE WHEN t.status = 'EDITING' THEN '编辑中' WHEN t.status = 'AUDITING' THEN '待审核' WHEN t.status = 'REJECTED' THEN '驳回' WHEN t.status = 'QUALIFIED' THEN '合格' WHEN t.status = 'MODIFIED' THEN '变更申报' END AS status, ")
                     .append("t.length AS length, ")
                     .append("t.width AS width, ")
                     .append("t.height AS height, ")
                     .append("CASE WHEN t.trailer_type = 'EDITING' THEN '开放式' WHEN t.trailer_type = 'HALFCLOSED' THEN '半封闭' WHEN t.trailer_type = 'CLOSED' THEN '全封闭' WHEN t.trailer_type = 'FLYWING' THEN '飞翼挂' WHEN t.trailer_type = 'FLAT' THEN 'FLAT' WHEN t.trailer_type = 'HURDLE' THEN '高栏挂' END AS trailerType, ")
                     .append("CASE WHEN t.vehicle_type = 'HEAVY_CONTAINER' THEN '重型集装箱半挂车' WHEN t.vehicle_type = 'HEAVY_TRANSPORT_TRAILER' THEN '重型车辆运输半挂车' WHEN t.vehicle_type = 'HEAVY_SPECIAL_TRAILER' THEN '重型特殊结构半挂车' WHEN t.vehicle_type = 'HEAVY_MIDDLE_AXIS' THEN '重型中置轴车辆运输车' WHEN t.vehicle_type = 'HEAVY_BOX_TRUCK' THEN '重型箱式货车' WHEN t.vehicle_type = 'COMMON_TRAILER' THEN '普通挂车' WHEN t.vehicle_type = 'CANISTER_TRAILER' THEN '罐式挂车' WHEN t.vehicle_type = 'FLAT_TRAILER' THEN '平板挂车' WHEN t.vehicle_type = 'SELF_STEVEDORING' THEN '自装卸挂车' END AS vehicleType, ")
                     .append("t.trainload AS trainload, ")
                     .append("t.annual_inspection_date AS annualInspectionDate, ")
                     .append("t.axles AS axles, ")
                     .append("t.reject_reason AS rejectReason, ")
                     .append("t.declare_time AS declareTime, ")
                     .append("t.is_bind AS bind, ")
                     .append("t.brand AS brand ")
              .append("FROM fms_trailer t, fms_supplier s ")
              .append("WHERE t.is_disabled = 0 AND s.is_disabled = 0 AND t.supplier_id = s.id")
              .append(getPreparedFilter(false).clauseSentence() + queryOrder.sentence());
        
        return buffer.toString();
        
    }
    
    /**
     * build rowMapper
     * 
     * @return
     */
    public RowMapper<Map<String, Object>> buildRowMapper() {
        
        RowMapper<Map<String, Object>> rowMapper = new RowMapper<Map<String,Object>>() {

            /* (non-Javadoc)
             * @see org.springframework.jdbc.core.RowMapper#mapRow(java.sql.ResultSet, int)
             */
            public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
                
                Map<String, Object> result = new WeakHashMap<String, Object>();
                
                result.put("id", rs.getObject("id"));
                result.put("brandCnCode", rs.getObject("brandCnCode"));
                result.put("floorLord", rs.getObject("floorLord"));
                result.put("vincode", rs.getObject("vincode"));
                result.put("supplierName", rs.getObject("supplierName"));
                result.put("supplierSimpleName", rs.getObject("supplierSimpleName"));
                result.put("status", rs.getObject("status"));
                result.put("length", rs.getObject("length"));
                result.put("width", rs.getObject("width"));
                result.put("height", rs.getObject("height"));   
                result.put("trailerType", rs.getObject("trailerType"));
                result.put("vehicleType", rs.getObject("vehicleType"));
                result.put("trainload", rs.getObject("trainload"));
                result.put("annualInspectionDate", rs.getObject("annualInspectionDate"));
                result.put("axles", rs.getObject("axles"));
                result.put("rejectReason", rs.getObject("rejectReason"));
                result.put("declareTime", rs.getObject("declareTime"));
                result.put("bind", rs.getBoolean("bind"));
                result.put("brand", rs.getObject("brand"));
                
                return result;
                
            }
            
        };
        
        return rowMapper;
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).parameters();
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public QueryOrder getOrder() {
        return queryOrder;
    }

    /**
     * @return the supplierId
     */
    public Long getSupplierId() {
        return supplierId;
    }

    /**
     * @return the status
     */
    public String getStatus() {
        return status;
    }

    /**
     * @param supplierId the supplierId to set
     */
    public void setSupplierId(Long supplierId) {
        this.supplierId = supplierId;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(String status) {
        this.status = status;
    }
    
}
